package controllers

import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import models.Tables._
import dao._
import tool.Tool
import utils.Utils

import scala.concurrent.ExecutionContext.Implicits.global


/**
 * Created by yz on 2019/1/16
 */
class TestController @Inject()(cc: ControllerComponents, trashDao: TrashDao, missionDao: MissionDao,
                               pdfInfoDao: PdfInfoDao, extraDataDao: ExtraDataDao, tool: Tool) extends AbstractController(cc) {

  def refreshTrash = Action.async {
    missionDao.selectAll.zip(trashDao.selectAll).flatMap { case (x, trashs) =>
      val rows = x.map(y =>TrashRow(y.id, y.userId, true)).filterNot(y => trashs.map(_.id).contains(y.id))
      trashDao.insertAll(rows)
    }.map { x =>
      Ok("success!")
    }
  }

  def deleteUserData = Action.async { implicit request =>
    missionDao.deleteAll.zip(trashDao.deleteAll).zip(extraDataDao.deleteAll).map { x =>
      val missionDir = tool.getUserMissionDir
      missionDir.listFiles().foreach { dir =>
        Utils.deleteDirectory(dir)
      }
      Ok("Delete success!")
    }

  }


}
